package com.xsy.android.compose.wandroid.ui.page.mine

import androidx.compose.foundation.Image
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.wrapContentWidth
import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.verticalScroll
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.automirrored.filled.KeyboardArrowRight
import androidx.compose.material3.Button
import androidx.compose.material3.HorizontalDivider
import androidx.compose.material3.Icon
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import com.xsy.android.compose.wandroid.Play
import com.xsy.android.compose.wandroid.R
import com.xsy.android.compose.wandroid.logic.model.ArticleModel
import com.xsy.android.compose.wandroid.logic.utils.DataStoreUtils
import com.xsy.android.compose.wandroid.ui.page.login.LogoutDefault
import com.xsy.android.compose.wandroid.ui.page.login.LogoutFinish
import com.xsy.android.compose.wandroid.ui.page.login.LogoutSate

/**
 *@author: xiaosy
 *时间: 2024/7/4
 *@Description: java类作用描述
 *@Version: 1.0
 **/
@Composable
fun ProfilePage(
    modifier: Modifier = Modifier, toLogin: () -> Unit,
    enterArticle: (ArticleModel) -> Unit,
    logoutState: LogoutSate,
    logout: () -> Unit
) {
    Column(
        modifier = modifier
            .fillMaxSize()
            .verticalScroll(rememberScrollState())
    ) {
        Image(
            painter = painterResource(id = R.mipmap.img_head),
            contentDescription = null,
            modifier = Modifier.fillMaxWidth(),
            contentScale = ContentScale.Crop
        )
        UserInfoFields(
            toLogin = toLogin,
            enterArticle = enterArticle,
            logoutState = logoutState,
            logout = logout
        )
    }
}


@Composable
fun UserInfoFields(
    modifier: Modifier = Modifier,
    toLogin: () -> Unit,
    enterArticle: (ArticleModel) -> Unit,
    logoutState: LogoutSate,
    logout: () -> Unit
) {
    Column {
        Spacer(modifier = modifier.height(8.dp))
        when (logoutState) {
            LogoutDefault -> {
                NameAndPosition(refresh = true, toLogin = toLogin)
            }

            LogoutFinish -> {
                NameAndPosition(refresh = false, toLogin = toLogin)
            }
        }

        ProfileProperty(
            article = ArticleModel(
                title = stringResource(id = R.string.mine_blog),
                link = "https://zhujiang.blog.csdn.net/"
            ),
            enterArticle = enterArticle
        )
        ProfileProperty(
            article = ArticleModel(
                title = stringResource(id = R.string.mine_nuggets),
                link = "https://juejin.im/user/5c07e51de51d451de84324d5"
            ),
            enterArticle = enterArticle
        )
        ProfileProperty(
            article = ArticleModel(
                title = stringResource(id = R.string.mine_github),
                link = "https://github.com/zhujiang521"
            ),
            enterArticle = enterArticle
        )

        if (Play.isLogin) {
            Button(
                onClick = { logout() }, modifier = Modifier
                    .fillMaxWidth()
                    .padding(24.dp)
            ) {
                Text(text = stringResource(id = R.string.log_out))
            }
        }
    }
}

@Composable
fun NameAndPosition(modifier: Modifier = Modifier, refresh: Boolean, toLogin: () -> Unit) {
    Column(modifier = if (Play.isLogin) {
        Modifier.padding(horizontal = 16.dp)
    } else {
        Modifier
            .padding(horizontal = 16.dp)
            .clickable {
                toLogin()
            }
    }) {
        Text(
            text = if (Play.isLogin && refresh) Play.nickname else stringResource(id = R.string.no_login),
            modifier = Modifier.height(32.dp),
            style = MaterialTheme.typography.titleLarge
        )
        Text(
            text = if (Play.isLogin && refresh) Play.username else stringResource(id = R.string.click_login),
            modifier = Modifier
                .padding(bottom = 20.dp)
                .height(24.dp),
            style = MaterialTheme.typography.bodyLarge
        )

    }
}


@Preview(showBackground = true)
@Composable
private fun PreviewNameAndPosition() {
    val context = LocalContext.current
    DataStoreUtils.init(context)
    val dataStore = DataStoreUtils
    Play.initialize(dataStore)
    NameAndPosition(refresh = true) {}
}

@Composable
fun ProfileProperty(article: ArticleModel, enterArticle: (ArticleModel) -> Unit) {
    Column(modifier = Modifier.clickable {
        enterArticle(article)
    }) {
        HorizontalDivider()
        Row(
            modifier = Modifier
                .fillMaxWidth()
                .height(56.dp)
                .padding(16.dp),
            verticalAlignment = Alignment.CenterVertically,
        ) {
            Text(
                text = article.title,
                modifier = Modifier.weight(1f),
                style = MaterialTheme.typography.bodyMedium
            )
            Icon(
                imageVector = Icons.AutoMirrored.Filled.KeyboardArrowRight,
                contentDescription = "",
                modifier = Modifier.wrapContentWidth(Alignment.End)
            )
        }
    }
}


@Preview(showBackground = true)
@Composable
private fun PreviewProfileProperty() {

    ProfileProperty(article = ArticleModel(title = "测试xxxxxxx")) {}
}

@Preview
@Composable
private fun PreviewProfilePage() {
//    ProfilePage()
}